package cc.git.liuyan.customeraiagent.core.bigmodel;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class PromptUtil {
    public static String format(String promptTemplate, String... params) {
        //找到模板中参数的个数
        String escapedTarget = Pattern.quote("%s");
        Pattern pattern = Pattern.compile(escapedTarget);
        Matcher matcher = pattern.matcher(promptTemplate);
        int count = 0;
        while (matcher.find()) {
            count++;
        }
        if (count == 0) {
            if (params == null || params.length == 0) {
                return promptTemplate;
            } else {
                throw new IllegalArgumentException("params length=" + params.length + " but %s count is empty");
            }

        } else {
            if (params == null || params.length == 0) {
                throw new IllegalArgumentException("%s count=" + count + " but params is null or empty");
            } else if (count != params.length) {
                throw new IllegalArgumentException("%s count=" + count + " but params length=" + params.length);
            } else {
                return String.format(promptTemplate, params);
            }
        }
    }
}
